package com.rnmeituan;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.util.Log;
import android.widget.Toast;

import com.facebook.react.bridge.ActivityEventListener;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.modules.core.DeviceEventManagerModule;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Nullable;

/**
 * Created by WangYu on 2017/8/23.
 */

public class MyNativeMoudle extends ReactContextBaseJavaModule implements ActivityEventListener, LifecycleEventListener {
    private ReactApplicationContext context;

    public MyNativeMoudle(ReactApplicationContext reactContext) {
        super(reactContext);
        context = reactContext;
        context.addLifecycleEventListener(this);
    }

    @Override
    public String getName() {
        return "MyNativeMoudle";
    }

    @Nullable
    @Override
    public Map<String, Object> getConstants() {
        //可以给rn提供常量
        Map<String, Object> map = new HashMap<>();
        map.put("url", "www.url.com");
        return map;
    }

    @ReactMethod
    public void rnCallNative(String msg) {
//        Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
//        context.addActivityEventListener(this);
//        Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
//        context.startActivityForResult(intent, 0, new Bundle());

        Intent intent = null;
        try {
            intent = new Intent("android.intent.action.VIEW");
            intent.addCategory("android.intent.category.DEFAULT");
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            Uri uri = Uri.fromFile(new File(msg));
            intent.setDataAndType(uri, "application/msword");
        } catch (Exception e) {
            e.printStackTrace();
        }
        context.startActivity(intent);
    }
    @ReactMethod
    public void exitApp(){
        MainApplication.exit();
    }

    @ReactMethod
    public void method1(Callback callback1){
        callback1.invoke(100,200,300,"王宇");
    }

    @ReactMethod
    public void promise(String msg, Promise promise) {
        try {
            Toast.makeText(context,msg,Toast.LENGTH_SHORT).show();
            promise.resolve("已经成功弹出了土司");
        } catch (Exception e) {
            e.printStackTrace();
            promise.resolve(e.toString());
        }
    }

    public void sendMessageToRn(String msg){
        Log.i("wangyu", msg);
        context.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
                .emit("Android",msg);
    }

    @Override
    public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
        sendMessageToRn("先回调了这里");
    }

    @Override
    public void onNewIntent(Intent intent) {

    }

    @Override
    public void onHostResume() {

    }

    @Override
    public void onHostPause() {

    }

    @Override
    public void onHostDestroy() {

    }
}
